package com.erhuogo.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.ReportDao;
import com.erhuogo.entity.Report;

/**
 * 举报Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:48:15
 */
public class ReportDaoImpl extends HibernateDaoSupport implements ReportDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ReportDaoImpl.class);

	// 1.分页条件查询举报信息记录数Dao实现类
	@Override
	public int countByParams(String id, String commodityIdStr, String acountIdStr, String typeStr, String handleStatusStr,
			String startDate, String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Report.class);
		if(!StringUtils.isEmpty(id)){
			Integer reportId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(reportId));
		}
		if(!StringUtils.isEmpty(commodityIdStr)){
			Integer commodityId = Integer.valueOf(commodityIdStr);
			criteria.add(Restrictions.eq("commodityId", commodityId));
		}
		if(!StringUtils.isEmpty(acountIdStr)){
			Integer acountId = Integer.valueOf(acountIdStr);
			criteria.add(Restrictions.eq("acountId", acountId));
		}
		if(!StringUtils.isEmpty(typeStr)){
			Integer type = Integer.valueOf(typeStr);
			criteria.add(Restrictions.eq("type", type));
		}
		if(!StringUtils.isEmpty(handleStatusStr)){
			Integer handleStatus = Integer.valueOf(handleStatusStr);
			criteria.add(Restrictions.eq("handleStatus", handleStatus));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("time", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Report> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的举报信息");
		} else{
			logger.info("没有找到符合查询条件的举报信息");
		}
		return count;
	}

	// 2.分页条件查询举报信息记录集合Dao实现类
	@Override
	public List<Report> findPageByParams(String id, String commodityIdStr, String acountIdStr, String typeStr,
			String handleStatusStr, String startDate, String endDate) {
		List<Report> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Report.class);
		if(!StringUtils.isEmpty(id)){
			Integer reportId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(reportId));
		}
		if(!StringUtils.isEmpty(commodityIdStr)){
			Integer commodityId = Integer.valueOf(commodityIdStr);
			criteria.add(Restrictions.eq("commodityId", commodityId));
		}
		if(!StringUtils.isEmpty(acountIdStr)){
			Integer acountId = Integer.valueOf(acountIdStr);
			criteria.add(Restrictions.eq("acountId", acountId));
		}
		if(!StringUtils.isEmpty(typeStr)){
			Integer type = Integer.valueOf(typeStr);
			criteria.add(Restrictions.eq("type", type));
		}
		if(!StringUtils.isEmpty(handleStatusStr)){
			Integer handleStatus = Integer.valueOf(handleStatusStr);
			criteria.add(Restrictions.eq("handleStatus", handleStatus));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("time", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Report> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的举报信息");
		} else{
			logger.info("没有找到符合查询条件的举报信息");
		}
		return result;
	}

	// 3.更新举报信息的处理状态Dao实现类
	@Override
	public void updateStatus(Integer id, Integer statusCode) {
		String hql = "from Report where id = ?";
		List<Report> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			Report report = list.get(0);
			report.setHandleStatus(statusCode);
			this.getHibernateTemplate().saveOrUpdate(report);
		}
	}
}
